package org.ycsoft.metroid;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

import org.ycsoft.MetroidReader.MetroidMetadata;
import org.ycsoft.MetroidReader.MetroidReader;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class MetroidPrefs extends Activity{
	
	public TextView title, lang, chooseMap, notif, notifFolder;
	public Spinner lang_spn;
	public Spinner maps;
	public Button cancel, validate;
	private int numberMaps;
	//public List<String> mapFiles = new ArrayList<String>();
	public List<String> displayNames = new ArrayList<String>();
	public List<String> mapFilesList = new ArrayList<String>();
	public List<String> listLang=new ArrayList<String>();
	public AutoCompleteTextView browseFolder;
	private List<String> directoryEntries = new ArrayList<String>();
	SharedPreferences prefs;
	File root1=new File("/data/data/org.ycsoft.metroid/");
	//File root2=new File("/data/download/");
	//File root=new File("/data/data");
	//Root1 and Root2 are possible locations for users files
	
	@Override
	public void onCreate(Bundle icicle)
	{         
		super.onCreate(icicle);
		setContentView(R.layout.prefs);
		title=(TextView)findViewById(R.id.title_prefs);
		lang=(TextView)findViewById(R.id.txtlang_prefs);
		lang_spn=(Spinner)findViewById(R.id.lang_prefs);
		chooseMap=(TextView)findViewById(R.id.txtmap_prefs);
		notif=(TextView)findViewById(R.id.txtnotif_prefs);
		maps=(Spinner)findViewById(R.id.maps_prefs);
		cancel=(Button)findViewById(R.id.quit_prefs);
		validate=(Button)findViewById(R.id.ok_prefs);
		browseFolder=(AutoCompleteTextView)findViewById(R.id.folder_prefs);
		notifFolder=(TextView)findViewById(R.id.txtfolder_prefs);
		notifFolder.setText(R.string.def_folder);
		title.setText(R.string.prefs_title);
		lang.setText(R.string.def_lang);
		chooseMap.setText(R.string.choose_city);
		notif.setText(R.string.def_map);
		prefs=getSharedPreferences(Commons.PREFS_NAME,0);
		browseFolder.setText(prefs.getString(Commons.PREFS_FOLDER,Commons.PREFS_DEF_FOLDER));
		fillFields();
				
	}
	
	public void fillFields() {
		try {
			String rep = prefs.getString(Commons.PREFS_FOLDER, 
					Commons.PREFS_DEF_FOLDER);
			File dir = new File(rep);
			File[] files = dir.listFiles(filtre);
			if (files == null || files.length == 0) {
				numberMaps = 0;
				displayNames.add(getResources().getString(R.string.no_maps)); // test if package exists
			} else {
				numberMaps = 0;
				for (File map : files) {
					MetroidReader reader = new MetroidReader(map.getAbsolutePath());
					reader.loadIndexes(false);
					MetroidMetadata meta = reader.getMetadata();
					reader.closeStream();
					displayNames.add(meta.getCity()/*+
							Commons.KEY_SEP+map.getName()*/);
					mapFilesList.add(map.getAbsolutePath());
					numberMaps++;
				}
			}

		} catch (Exception ioe) {
			ioe.printStackTrace();
		}

		ArrayAdapter<String> mapItems = new ArrayAdapter<String>(this,
				R.layout.list, displayNames);
		maps.setAdapter(mapItems);
		maps.setSelection(mapFilesList.indexOf(prefs.getString(Commons.PREFS_MAPFILE,Commons.PREFS_DEF_MAPFILE)),true);
		
		listLang.add("French");
		listLang.add("English");
		ArrayAdapter<String> langItems = new ArrayAdapter<String>(this,
				R.layout.list, listLang);
		lang_spn.setAdapter(langItems);
		lang_spn.setSelection(listLang.indexOf(prefs.getString(Commons.PREFS_LANG,Commons.PREFS_DEF_LANG)));
		cancel.setOnClickListener(cancelListener);
		validate.setOnClickListener(validListener);
		//File system browsing
		populateFileBrowser(root1);
		//populateFileBrowser(root2);
		//populateFileBrowser(root);
		  browseFolder.setAdapter(new ArrayAdapter<String>(this,R.layout.list,directoryEntries));
	}
	
	public void populateFileBrowser(File f)
	{
		  for(File file:f.listFiles())
			{
				if(file.isDirectory())
				{
					directoryEntries.add(file.getPath());
					populateFileBrowser(file);					
				}	
			}
	}

	/*
	 * Filter for metroid files (.mtd extensions)
	 */
	FilenameFilter filtre = new FilenameFilter() {

		public boolean accept(File dir, String filename) {
			return filename.endsWith(".mtd");
		}
	};
	
	private OnClickListener cancelListener = new OnClickListener(){
		public void onClick(View v){
			//close activity
			finish();
		}
	};
	
	private OnClickListener validListener = new OnClickListener(){
		public void onClick(View v){
			
		      SharedPreferences settings = getSharedPreferences(
		    		  Commons.PREFS_NAME, 0);
		      SharedPreferences.Editor editor = settings.edit();
		      
		      editor.putString(Commons.PREFS_LANG,
		    		  lang_spn.getSelectedItem().toString());
		      editor.putString(Commons.PREFS_CITY,
		    		  maps.getSelectedItem().toString());
		      editor.putString(Commons.PREFS_FOLDER,browseFolder.getText().toString());
		      
		      editor.putString(Commons.PREFS_MAPFILE, mapFilesList.get(maps.getSelectedItemPosition()));
		      
		      editor.commit();
			//close activity
		      finish();
		}
	};

	
}